package com.ruoyi.worker.service.impl;

import com.repair.api.worker.request.WorkerBlockRequest;
import com.repair.api.worker.request.WorkerQueryPageRequest;
import com.repair.api.worker.request.WorkerQueryRequest;
import com.repair.api.worker.request.param.WorkerQueryParam;
import com.repair.api.worker.response.WorkerQueryResponse;
import com.repair.api.worker.response.data.WorkerInfo;
import com.repair.api.worker.service.WorkerFacadeService;
import com.repair.base.exception.BizErrorCode;
import com.repair.base.exception.BizException;
import com.repair.base.response.PageResponse;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.worker.service.AdminWorkerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * Created by IntelliJ IDEA.
 *
 * @author zq
 * @version 1.0
 * @create 2024-10-20 19:19
 * @since jdk1.8
 */
@Slf4j
@Service
public class AdminWorkerServiceImpl implements AdminWorkerService {
    @DubboReference
    private WorkerFacadeService workerFacadeService;

    @Override
    public TableDataInfo getAllWorker(WorkerQueryParam workerQueryParam) {
        // 打印调试日志，记录获取师傅列表的查询参数
        log.debug("获取全部师傅{}", workerQueryParam);
        // 创建分页查询请求对象
        WorkerQueryPageRequest<WorkerQueryParam> pageRequest = new WorkerQueryPageRequest<>();
        // 设置当前页码
        pageRequest.setCurrentPage(workerQueryParam.getCurrentPage());
        // 设置每页记录数
        pageRequest.setPageSize(workerQueryParam.getPageSize());
        // 设置查询参数
        pageRequest.setData(workerQueryParam);
        // 调用服务接口获取所有师傅的信息
        PageResponse<WorkerInfo> allWorkers = workerFacadeService.getAllWorkers(pageRequest);
        // 检查获取师傅信息是否成功，如果不成功则抛出业务异常
        if (!allWorkers.getSuccess()) {
            throw new BizException(BizErrorCode.REMOTE_CALL_RESPONSE_IS_FAILED);
        }
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(allWorkers.getTotal());
        tableDataInfo.setRows(allWorkers.getData());
        return tableDataInfo;
    }

    @Override
    public void blockWorker(Long id) {
        // 创建一个WorkerInfo实例来存储师傅信息
        WorkerInfo workerInfo = new WorkerInfo();
        // 设置师傅的ID
        workerInfo.setId(id);

        workerFacadeService.blockWorker(new WorkerBlockRequest<>(workerInfo.getId()));
        // 创建一个查询请求，根据师傅状态查询师傅信息
        WorkerQueryResponse<WorkerInfo> query = workerFacadeService.query(new WorkerQueryRequest(workerInfo.getId()));
        // 检查查询是否成功
        if (!query.getSuccess()) {
            // 如果查询失败，抛出业务异常，表明保存通知失败
            throw new BizException(BizErrorCode.NOTICE_SAVE_FAILED);
        }
    }

    @Override
    public TableDataInfo getBlockWorker(WorkerQueryParam workerQueryParam) {
        // 打印调试日志，记录获取师傅列表的查询参数
        log.debug("获取全部拉黑师傅{}", workerQueryParam);
        // 创建分页查询请求对象
        WorkerQueryPageRequest<WorkerQueryParam> pageRequest = new WorkerQueryPageRequest<>();
        // 设置当前页码
        pageRequest.setCurrentPage(workerQueryParam.getCurrentPage());
        // 设置每页记录数
        pageRequest.setPageSize(workerQueryParam.getPageSize());
        // 设置查询参数
        workerQueryParam.setWorkerStatus("BLOCKED");
        pageRequest.setData(workerQueryParam);
        // 调用服务接口获取所有师傅的信息
        PageResponse<WorkerInfo> allWorkers = workerFacadeService.getAllWorkers(pageRequest);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(allWorkers.getTotal());
        tableDataInfo.setRows(allWorkers.getData());
        return tableDataInfo;
    }

    @Override
    public void cancelBlockWorker(Long id) {
        // 创建一个WorkerInfo实例来存储师傅信息
        WorkerInfo workerInfo = new WorkerInfo();
        // 设置师傅的ID
        workerInfo.setId(id);

        workerFacadeService.cancelBlockWorker(new WorkerBlockRequest<>(workerInfo.getId()));
        // 创建一个查询请求，根据师傅状态查询师傅信息
        WorkerQueryResponse<WorkerInfo> query = workerFacadeService.query(new WorkerQueryRequest(workerInfo.getId()));
        // 检查查询是否成功
        if (!query.getSuccess()) {
            // 如果查询失败，抛出业务异常，表明保存通知失败
            throw new BizException(BizErrorCode.NOTICE_SAVE_FAILED);
        }
    }
}
